package com.agh.portal.service;

import com.agh.portal.data.Votable;
import com.agh.portal.domain.Answer;
import com.agh.portal.domain.Comment;
import com.agh.portal.domain.MainPagePost;
import com.agh.portal.domain.Post;
import com.agh.portal.domain.Question;
import com.agh.portal.domain.Reply;
import com.agh.portal.domain.Student;

public class PersistanceUtils {
	
	public static Votable addGoodVote(String entityName, Long id, Long studentId){
		Student student = Student.findStudent(studentId);
		Votable votableEntity = findVotableEntity(entityName, id);
		if(student == null || votableEntity == null) throw new IllegalArgumentException("Post or student not found");
		
		if(!votableEntity.getProVoters().contains(student)){
			//check if student voted opposite before, if yes - delete that vote
			if(votableEntity.getConVoters().contains(student)){
				votableEntity.getConVoters().remove(student);
				votableEntity.decrementBadVote();
			}
			votableEntity.getProVoters().add(student);
			votableEntity.addGoodVote();
			votableEntity.persist();
		}
		return votableEntity;
	}
	
	public static Votable addBadVote(String entityName, Long id, Long studentId){
		Student student = Student.findStudent(studentId);
		Votable votableEntity = findVotableEntity(entityName, id);
		if(student == null || votableEntity == null) throw new IllegalArgumentException("Post or student not found");
		
		if(!votableEntity.getConVoters().contains(student)){
			//check if student voted opposite before, if yes - delete that vote
			if(votableEntity.getProVoters().contains(student)){
				votableEntity.getProVoters().remove(student);
				votableEntity.decrementGoodVote();
			}
			votableEntity.getConVoters().add(student);
			votableEntity.addBadVote();
			votableEntity.persist();
		}
		return votableEntity;
	}
	
	private static Votable findVotableEntity(String entityName, Long entityId){
		if(entityName.equals("mainpagepost")){
			return MainPagePost.findMainPagePost(entityId);
		}else if(entityName.equals("comment")){
			return Comment.findComment(entityId);
		}
		else if(entityName.equals("answer")){
			return Answer.findAnswer(entityId);
		}
		else if(entityName.equals("question")){
			return Question.findQuestion(entityId);
		}
		return null;
	}
	
		
	public static MainPagePost addMainPagePostGoodVote(Long id, Long studentId){
		Student student = Student.findStudent(studentId);
		MainPagePost post = MainPagePost.findMainPagePost(id);
		if(student == null || post == null) throw new IllegalArgumentException("Post or student not found");
		
		if(!post.getProVoters().contains(student)){
			//check if student voted opposite before, if yes - delete that vote
			if(post.getConVoters().contains(student)){
				post.getConVoters().remove(student);
				post.decrementBadVote();
			}
			post.getProVoters().add(student);
			post.addGoodVote();
			post.persist();
		}
		return post;
	}

	public static MainPagePost addMainPagePostBadVote(Long id, Long studentId){
		Student student = Student.findStudent(studentId);
		MainPagePost post = MainPagePost.findMainPagePost(id);
		if(student == null || post == null) throw new IllegalArgumentException("Post or student not found");
		
		if(!post.getConVoters().contains(student)){
			//check if student voted opposite before, if yes - delete that vote
			if(post.getProVoters().contains(student)){
				post.getProVoters().remove(student);
				post.decrementGoodVote();
			}
			post.getConVoters().add(student);
			post.addBadVote();
			post.persist();
		}
		return post;
	}
	
}
